package com.ruoyi.classai.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.classai.domain.PxTraining;
import com.ruoyi.classai.domain.PxTrainingCourse;
import com.ruoyi.classai.domain.PxTrainingTeacher;
import com.ruoyi.classai.domain.vo.px.PxMyscoreVo;
import com.ruoyi.classai.domain.vo.px.PxScoreRecordVo;
import com.ruoyi.classai.domain.vo.px.PxTrainingVo;
import com.ruoyi.classai.mapper.PxTrainingMapper;
import com.ruoyi.classai.service.IPxTrainingService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
 * 培训信息Service业务层处理
 * 
 * @author lcj
 * @date 2025-06-17
 */
@Service
public class PxTrainingServiceImpl extends MPJBaseServiceImpl<PxTrainingMapper, PxTraining> implements IPxTrainingService {

    @Autowired
    private PxTrainingMapper pxTrainingMapper;

    @Override
    public List<PxTrainingVo> getTrainingList(String trainingTitle, Integer trainingType,Integer trainingStatus,String teacherId) {
        MPJLambdaWrapper<PxTraining> wrapper= new MPJLambdaWrapper<>();
        wrapper.selectAll(PxTraining.class)
                .select(PxTrainingTeacher::getTrainingProgress)
                .innerJoin(PxTrainingTeacher.class,PxTrainingTeacher::getTrainingId,PxTraining::getId)
                .eq(PxTrainingTeacher::getTeacherId, teacherId)
                .eq(PxTraining::getStatus, Constants.COMMON_SUCCESS)
                .ge(Objects.isNull(trainingStatus),PxTraining::getEndTime,DateUtil.date()) //结束时间大于等于当前时间
                .le(Objects.nonNull(trainingStatus)&&trainingStatus==1,PxTraining::getStartTime,DateUtil.date()) //开始时间小于等于当前时间
                .ge(Objects.nonNull(trainingStatus)&&trainingStatus==1,PxTraining::getEndTime,DateUtil.date()) //结束时间大于等于当前时间
                .le(Objects.nonNull(trainingStatus)&&trainingStatus==2,PxTraining::getEndTime,DateUtil.date()) //结束时间小于等于当前时间
                .ge(Objects.nonNull(trainingStatus)&&trainingStatus==0,PxTraining::getStartTime,DateUtil.date()) //开始时间大于等于当前时间
                .like(StringUtils.isNotEmpty(trainingTitle),PxTraining::getTrainingTitle,trainingTitle)
                .eq(Objects.nonNull(trainingType),PxTraining::getTrainingType,trainingType)
                .orderByAsc(PxTraining::getStartTime);
        // 执行查询并返回结果列表
        return this.selectJoinList(PxTrainingVo.class, wrapper);
    }

    @Override
    public PxTrainingVo getTrainingById(String trainingId, String teacherId) {
        MPJLambdaWrapper<PxTraining> wrapper= new MPJLambdaWrapper<>();
        wrapper.selectAll(PxTraining.class)
                .select(PxTrainingTeacher::getTrainingProgress)
                .innerJoin(PxTrainingTeacher.class,PxTrainingTeacher::getTrainingId,PxTraining::getId)
                .eq(PxTrainingTeacher::getTeacherId, teacherId)
                .eq(PxTraining::getId, trainingId)
                .eq(PxTraining::getStatus, Constants.COMMON_SUCCESS);
        // 执行查询并返回结果列表
        return this.selectJoinOne(PxTrainingVo.class, wrapper);
    }

    @Override
    public PxMyscoreVo getMyScore(String teacherId) {
        //查询学分记录
        MPJLambdaWrapper<PxTraining> wrapper= new MPJLambdaWrapper<>();
        wrapper.select(PxTraining::getId,PxTraining::getTrainingTitle,PxTraining::getTrainingType)
                .select(PxTrainingTeacher::getTrainingScore,PxTrainingTeacher::getUpdateTime)
                .innerJoin(PxTrainingTeacher.class,PxTrainingTeacher::getTrainingId,PxTraining::getId)
                .eq(PxTrainingTeacher::getTeacherId, teacherId)
                .eq(PxTraining::getStatus, Constants.COMMON_SUCCESS)
                .ne(PxTrainingTeacher::getTrainingScore,0)
                .orderByDesc(PxTraining::getCreateTime);

        // 执行查询并返回结果列表
        PxMyscoreVo myscoreVo=new PxMyscoreVo();
        List<PxScoreRecordVo> list=this.selectJoinList(PxScoreRecordVo.class,wrapper);
        int totalScore=0;
        int compulsoryScore=0;
        int practiceScore=0;
        if (CollectionUtil.isNotEmpty(list)){
            for (PxScoreRecordVo pxScoreRecordVo : list) {
                if (pxScoreRecordVo.getTrainingType()==0){ //必修
                    compulsoryScore=compulsoryScore+pxScoreRecordVo.getTrainingScore();
                }else { //实践
                    practiceScore=practiceScore+pxScoreRecordVo.getTrainingScore();
                }
                totalScore=totalScore+pxScoreRecordVo.getTrainingScore();
            }
        }
        myscoreVo.setCompulsoryScore(compulsoryScore);
        myscoreVo.setPracticeScore(practiceScore);
        myscoreVo.setTotalScore(totalScore);
        myscoreVo.setScoreRecordList(list);
        return myscoreVo;
    }

    @Override
    public List<String> getCourseTable(String month,String teacherId) {
        return pxTrainingMapper.getCourseTable(StringUtils.isNotEmpty(month)?month:DateUtil.format(DateUtil.date(),"yyyy-MM"),teacherId);
    }

    @Override
    public List<PxTrainingCourse> getCourseTableByDate(String teacherId, String date) {
        return pxTrainingMapper.getCourseTableByDate(teacherId,date);
    }
}
